package cn.impl;

import cn.annotation.Log;
import cn.basic.HibBaseDAOImpl;
import cn.basic.iHibBaseDAO;
import cn.common.properties.LogOperType;
import cn.dao.ClassRoomDAO;
import cn.model.TClassRoom;
import org.springframework.stereotype.Component;

import java.util.List;
@Component("classromedao")
public class ClassRoomDAOImpl implements ClassRoomDAO {

    //连接数据库操作层接口,实现业务与数据的交换
    private iHibBaseDAO bdao= null;

    public ClassRoomDAOImpl(){
        bdao = new HibBaseDAOImpl();
    }

    /*
     * 添加一个教室
     * @param TClassRoom 对象
     * @return 添加规则的教室编号，如<=0则表示添加失败
     */
    @Log(isSaveLog=true,operationType= LogOperType.ADD,operationName="添加一条教室信息")
    @Override
    public int addClass(TClassRoom room) {
        return (Integer) bdao.insert(room);
    }

    @Log(isSaveLog=true,operationType= LogOperType.DELETE,operationName="删除一条教室信息")
    @Override
    public boolean deleteClass(int classroomid) {
        return bdao.delete(classroomid);
    }

    @Log(isSaveLog=true,operationType= LogOperType.MODIFY,operationName="修改一条教室信息")
    @Override
    public boolean modifyClass(TClassRoom room) {
        return bdao.update(room);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询教室编号")
    @Override
    public TClassRoom getClassById(int classroomid) {
        return (TClassRoom) bdao.findById(TClassRoom.class,classroomid);
    }

    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="查询教室信息列表")
    @Override
    public List<TClassRoom> getClassListByPage(String wherecondition, int currentPage, int pageSize) {
        String hql = "from TClassRoom";
        if (wherecondition!=null && !wherecondition.equals(""))
        {
            hql+=wherecondition;
        }
        if (wherecondition==null && wherecondition.equals("")){
            hql += "order by createtime desc";
        }
        return bdao.selectByPage(hql,currentPage,pageSize);
    }

    /**
     * 根据状态查询TClassRoom对象
     *
     * @param state     状态int
     * @param currentPage 查询开始的页数
     * @param pageSize  查询要求的每页的记录数
     * @return List<VOrder>
     */
    @Log(isSaveLog=true,operationType= LogOperType.QUERY,operationName="通过教室状态查询教室信息息列表")
    @Override
    public List<TClassRoom> getTClassRoomByStatus(int state, int currentPage, int pageSize) {
        String hql = "from TClassRoom where state = ?";
        Object[] para = {state};
        return bdao.selectByPage(hql, para, currentPage, pageSize);
    }


    /**
     * 获得所有的规则并按pageSize显示的页数
     * 参数：每页显示数量
     * 返回值：分页总数
     *
     * @param wherecondition
     */
    @Override
    public int getAllClassPageAmount(String wherecondition) {
        String hql = "select count(*) from  TClassRoom";
        if (wherecondition!=null && !wherecondition.equals("")){
            hql+=wherecondition;
        }
        return bdao.selectValue(hql);
    }

    @Override
    public List<TClassRoom> getAllClasss() {
        String hql ="from TClassRoom";
        return bdao.select(hql);
    }

    @Override
    public List<TClassRoom> getClassListByPage(int currentPage, int pageSize) {
        String hql ="from TClassRoom";
        return bdao.selectByPage(hql,currentPage,pageSize);
    }

    /**
     * 根据条件获取符合条件的教室记录的数量
     *
     * @return 返回查询得到的记录条数
     */
    @Override
    public int getClassAmount() {
        String hql = "select count(*) from TClassRoom";
        return bdao.selectValue(hql);
    }
}
